package org.example.demo3.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.demo3.dto.SearchrecordDto;
import org.example.demo3.service.ProductService;
import org.example.demo3.service.SearchRecordService;
import org.example.demo3.vo.productVO;
import org.example.demo3.vo.result;
import org.example.demo3.vo.searchRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("search")

public class SearchController {
    @Autowired
    private SearchRecordService searchRecordService;

    @Autowired
    private ProductService productService;

    @PostMapping("find-record")
    public result<List<searchRecordVO>> findSearchRecord(@Validated @RequestBody SearchrecordDto searchrecorddto) {
        List<searchRecordVO> records = searchRecordService.find(searchrecorddto);
        return result.success(records);
    }

    @GetMapping("add")
    public result add(@Validated @RequestBody SearchrecordDto searchrecorddto) {
           searchRecordService.save(searchrecorddto);
           return result.success();
    }
    @DeleteMapping("clean")
    public result delete(@Validated @RequestBody SearchrecordDto searchrecorddto) {
        if(!searchRecordService.cleanSearchRecord(searchrecorddto)) {
            return result.fail("删除失败");
        }
        return result.success();
    }
    @PostMapping("search/{pageNum}/{pageSize}")
    public result<Page<productVO>> findSearchInsure(
            @RequestBody productVO productVo,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        Page<productVO> productPageVO = productService.findPage(productVo, pageNum, pageSize);
        return result.success(productPageVO);
    }


}
